package com.leyou.page.controller;

import com.leyou.page.service.FileService;
import com.leyou.page.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Map;

@Controller
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private FileService fileService;

    /**
     * 跳转到商品详情页
     * @param model
     * @param spuId
     * @return
     */
    @GetMapping("item/{spuId}.html")
    public String toItemPage(Model model, @PathVariable("spuId") Long spuId) {

        Map<String, Object> map = this.goodsService.loadModel(spuId);

        model.addAllAttributes(map);

        if (!this.fileService.exists(spuId)) {
            this.fileService.syncCreateHtml(spuId);
        }

        return "item";
    }


}
